package cn.wolfcode.p2p.web.controller;

import cn.wolfcode.p2p.base.domain.UserInfo;
import cn.wolfcode.p2p.base.service.ILoginInfoService;
import cn.wolfcode.p2p.base.service.IUserInfoService;
import cn.wolfcode.p2p.base.service.IVideoAuthService;
import cn.wolfcode.p2p.base.util.DateUtil;
import cn.wolfcode.p2p.base.util.JSONResult;
import cn.wolfcode.p2p.web.util.RequiredLogin;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.Date;
import java.util.List;

/**
 * 前台:实名认证页面
 */
@Controller
public class VideoAuthController {
    @Autowired
    private IVideoAuthService videoAuthService;
    @Autowired
    private IUserInfoService userInfoService;
    @Autowired
    private ILoginInfoService loginInfoService;

    // 进入实名认证申请页面/待审核页面/审核通过页面
    @RequestMapping("/videoAuth")
    @RequiredLogin
    public String toVideoAuthPage(Model model) {
        // 1.获取当前登录信息,判断当前是否已经认证通过
        UserInfo current = userInfoService.getCurrent();
        List<Date> appointmentDates = DateUtil.getLastDays(new Date(), 5);
        if (!current.getHasVideoAuth()) {
            // 2.没有通过实名认证
            if (current.getVideoAuthId() == null) {
                model.addAttribute("customServices", loginInfoService.getCustomeServices());
                model.addAttribute("appointmentDates", appointmentDates);
                return "videoAuth_apply";
            } else {
                model.addAttribute("auditing", true);
                model.addAttribute("videoAuth", videoAuthService.get(current.getVideoAuthId()));
                return "videoAuth_result";
            }
        } else {
            // 3.已经实名认证通过
            model.addAttribute("auditing", false);
            return "videoAuth_result";
        }
    }

    // 提交实名认证申请
    @PostMapping("/videoAudit_apply")
    @ResponseBody
    @RequiredLogin
    public JSONResult apply(String appointmentDate, String appointmentTime, Long customServiceId) {
        videoAuthService.apply(appointmentDate,appointmentTime,customServiceId);
        return new JSONResult();
    }


}
